<template>
  <div class="article-comments">
    <!-- 导航栏  "curComment.reply_count+'条回复'"-->
    <van-nav-bar :title="`${curComment.reply_count}条回复`">
      <van-icon slot="left" name="cross" @click="$emit('close')" />
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 当前评论 -->
    <van-cell title="当前评论">
      <van-image
        slot="icon"
        round
        width="30"
        height="30"
        style="margin-right: 10px;"
        :src="curComment.aut_photo"
      />
      <span style="color: #466b9d;" slot="title">{{
        curComment.aut_name
      }}</span>
      <div slot="label">
        <p style="color: #363636;">{{ curComment.content }}</p>
        <p>
          <span style="margin-right: 10px;">{{
            curComment.pubdate | relativeTime
          }}</span>
          <van-button size="mini" type="default"
            >回复 {{ curComment.reply_count }}</van-button
          >
        </p>
      </div>
      <van-icon slot="right-icon" />
    </van-cell>
    <!-- /当前评论 -->
    <van-divider>全部回复</van-divider>

    <!-- 对当前评论 回复列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item, index) in list"
        :key="index"
        :title="item.content"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636;">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{
              item.pubdate | relativeTime
            }}</span>
          </p>
        </div>
      </van-cell>
    </van-list>
    <!-- 回复列表 -->

    <!-- 发布回复 -->
    <van-cell-group class="publish-wrap">
      <van-field clearable placeholder="请输入回复内容" v-model.trim="content">
        <van-button
          slot="button"
          size="mini"
          type="info"
          @click="addCommentReply"
          >发布</van-button
        >
      </van-field>
    </van-cell-group>
    <!-- /发布回复 -->
  </div>
</template>
<script>
import { getComments, addComments } from "@/api/comment.js";
export default {
  name: "CommentReply",
  data() {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      content: ""
    };
  },
  props: {
    curComment: {
      type: Object,
      default() {
        return {};
      }
    },
    articleId: {
      type: String,
      default: ""
    }
  },
  methods: {
    async onLoad() {
      // 异步更新数据
      //   setTimeout(() => {
      //     for (let i = 0; i < 10; i++) {
      //       this.list.push(this.list.length + 1)
      //     }
      //     // 加载状态结束
      //     this.loading = false

      //     // 数据全部加载完成
      //     if (this.list.length >= 40) {
      //       this.finished = true
      //     }
      //   }, 500)

      // 参数
      const comm_id = this.curComment.com_id.toString(); // object
      const offset = this.offset;
      try {
        // 调用接口
        const res = await getComments({
          type: "c",
          source: comm_id,
          offset: offset,
          limit: 10
        });
        // 拿到数据并渲染
        const { results, last_id } = res.data.data;
        // results = []
        // last_id = '112434234'
        this.list.push(...results);
        this.offset = last_id && last_id.toString();
        // 处理vant-list 状态
        this.loading = false;
        if (results.length === 0) {
          this.finished = true;
        }
      } catch {
        // 如果接口错误那么这里认为加载完毕
        this.loading = false;
        this.finished = true;
        this.$toast.fail("服务器还没准备好");
      }
    },
    async addCommentReply() {
      // 参数 target对评论进行回复则为评论id
      // content, 评论内容
      // art_id  文章id，对评论内容发表回复时，需要传递此参数，表明所属文章id。
      const art_id = this.articleId,
        content = this.content,
        target = this.curComment.com_id.toString();
      try {
        // 请求服务器
        const res = await addComments({
          target,
          content,
          art_id
        });
        // 拿到数据并渲染
        const new_obj = res.data.data.new_obj;
        this.list.unshift(new_obj);
        // 处理当前组件的一些状态
        this.content = "";
        this.curComment.reply_count++;
        this.$toast.success("回复成功");
      } catch {
        // 接口有问题要提示回复失败
        this.$toast.fail("服务器还没准备好");
      }
    }
  }
};
</script>
<style lang="less" scoped>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
